﻿// ***********************************************************************
// Assembly         : MVC4Portal
// Author           : Lolo
// Created          : 10-12-2013
//
// Last Modified By : Lolo
// Last Modified On : 10-27-2013
// ***********************************************************************
// <copyright file="SingleFileUploadController.cs" company="">
//     Copyright (c) . All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using InnovaApp.Portals.MVC4Portal.Models.ViewModels;
using INNOVACALL.Framework.Common.Extensions;
using INNOVACALL.Framework.Common.MVCHelpers;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace InnovaApp.Portals.MVC4Portal.Controllers
{
    /// <summary>
    /// Class SingleFileUploadController.
    /// </summary>
    public class SingleFileUploadController : Controller
    {
        private string eMethodBase = "InnovaApp.Portals.MVC4Portal.Controllers.SingleFileUploadController";
        /// <summary>
        /// Uploads the user icon.
        /// ToDo LJO : Manage errors
        /// </summary>
        /// <param name="SingleFileUploadFileInput">The fil single file uploader.</param>
        /// <param name="SingleFileUploadImageToUpdateIdHidden">The img automatic update.</param>
        /// <returns>ActionResult.</returns>
        [HttpPost, Authorize, CacheManager(enCacheMode.NoCache)]
        public ActionResult UploadUserIcon(HttpPostedFileBase SingleFileUploadFileInput, string SingleFileUploadImageToUpdateIdHidden)
        {
            string eMethod = eMethodBase + "UploadUserIcon[Post]";
            string fileName = string.Format("{0}{1}", ((Guid)Membership.GetUser().ProviderUserKey).ToString(), Path.GetExtension(SingleFileUploadFileInput.FileName));
            //Save main icon
            ///string folderPath = Server.MapPath(InnovaApp.Layers.CommonLayer.Controllers.Environment.UserIconsFolder);
            string folderPath = PortalEnvironment.UserIconsPhysicalPath;
            folderPath.EnsurePathExists();
            string filePath = Path.Combine(new string[] { folderPath, fileName });
            //SingleFileUploadFileInput.SaveAs(filePath);


            Stream fileStm = SingleFileUploadFileInput.InputStream;
            using (FileStream outputStm = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                outputStm.Lock(0, fileStm.Length);
                Byte[] outputByte = new Byte[fileStm.Length];

                fileStm.Read(outputByte, 0, Convert.ToInt32(fileStm.Length));

                outputStm.Write(outputByte, 0, outputByte.Length);
                outputStm.Unlock(0, outputStm.Length);
                outputStm.Close();
            }


            UploadUserIcon_ViewModel model = new UploadUserIcon_ViewModel();
            model.UploadedFileUrl = string.Format("{0}/{1}", PortalEnvironment.UserIconsFolder, fileName);
            model.ImgToUpdate = SingleFileUploadImageToUpdateIdHidden;
            //Create Google map miniature
            using (Image image = Bitmap.FromFile(filePath))
            {
                ImageFormat imageOriginalFormat = image.GetImageFormat();
                Bitmap bitmap = new Bitmap(image);
                bitmap = bitmap.ResizeBitmap(44, 36, true);
                fileName = string.Format("{0}_google{1}", Path.GetFileNameWithoutExtension(filePath), Path.GetExtension(filePath));
                bitmap.Save(Path.Combine(folderPath, fileName), imageOriginalFormat);
            }
            return View(model);
        }
    }
}
